package com.example.mrzhou.lvmm.view.fragment.destination;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.mrzhou.lvmm.R;
import com.example.mrzhou.lvmm.commom.base.BaseFragment;
import com.example.mrzhou.lvmm.entity.destination.DesSendMsg;
import com.example.mrzhou.lvmm.entity.destination.Destination;
import com.example.mrzhou.lvmm.presenter.destination.DesPresenter;
import com.example.mrzhou.lvmm.view.adapter.destination.DesSampleAdapter;
import com.example.mrzhou.lvmm.view.impl.destination.DesView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class DesSampleFragment extends BaseFragment implements DesView {


    @BindView(R.id.xclv_des_sample)
    XRecyclerView xclvDesSample;
    private List<Destination.DatasBean.InfosBean> data = new ArrayList<>();
    private DesSampleAdapter adapter;
    public static final String KEY = "key";

    public DesSampleFragment() {
        // Required empty public constructor
    }

    public static DesSampleFragment newInstance(DesSendMsg msg) {

        Bundle args = new Bundle();
        args.putSerializable(KEY, msg);
        DesSampleFragment fragment = new DesSampleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_des_sample;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        xclvDesSample.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new DesSampleAdapter(data, getContext());
        xclvDesSample.setAdapter(adapter);
        xclvDesSample.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                data.clear();
                initData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        xclvDesSample.setRefreshing(true);
        xclvDesSample.setLoadingMoreEnabled(false);
    }

    /**
     * Content-Disposition: form-data; name="stationCode"
     * Content-Length: 2
     * <p>
     * SH
     * --d938de5d-d186-4a69-b403-22bf4baf5e65
     * Content-Disposition: form-data; name="channelCode"
     * Content-Length: 3
     * <p>
     * MDD
     * --d938de5d-d186-4a69-b403-22bf4baf5e65
     * Content-Disposition: form-data; name="tagCodes"
     * Content-Length: 6
     * <p>
     * MDD_GW
     * --d938de5d-d186-4a69-b403-22bf4baf5e65
     * Content-Disposition: form-data; name="page"
     * Content-Length: 1
     * <p>
     * 1
     * --d938de5d-d186-4a69-b403-22bf4baf5e65
     * Content-Disposition: form-data; name="pageSize"
     * Content-Length: 2
     * <p>
     * 20
     * --d938de5d-d186-4a69-b403-22bf4baf5e65—
     */
    @Override
    protected void initData() {
        DesSendMsg msg = (DesSendMsg) getArguments().getSerializable(KEY);
        DesPresenter presenter = new DesPresenter(this);
        Map<String, String> map = new HashMap<>();
        map.put("s", "/Api/getInfos");
        map.put("globalLatitude", "39.913248");
        map.put("globalLongitude", "116.403624");
//        map.put("clientTimestamp", "1477743040649");
//        map.put("lvtukey", "0c77c41e97c4604485775d66daf95852");
        map.put("clientTimestamp", msg.getClientTimestamp());
        map.put("lvtukey", msg.getLvtukey());
        map.put("osVersion", "4.4.2");
        map.put("lvversionCode", "65");
        map.put("lvversion", "7.7.3");
        map.put("deviceName", "V1.0");
        map.put("firstChannel", "ANDROID");
        map.put("udid", "864394010807818");
        map.put("formate", "json");
        map.put("secondChannel", "YYB");

        map.put("stationCode", "SH");
        map.put("channelCode", "MDD");
//        map.put("tagCodes", "MDD_GW");
        map.put("tagCodes", msg.getTagCodes());
        map.put("page", "1");
        map.put("pageSize", "20");

        presenter.getDesData(map);
    }

    @Override
    public void onSuccess(Destination destination) {
        if (destination != null) {
            data.addAll(destination.getDatas().get(0).getInfos());
            adapter.notifyDataSetChanged();
        }
        xclvDesSample.refreshComplete();
    }

    @Override
    public void onFaiure(Throwable a) {

    }

    @Override
    public void onFinsh() {

    }
}
